package com.easy;

/**
 * Created by Lxk on 2019/5/18.
 */
public class Solution234 {

    public boolean isPalindrome(ListNode head) {
        if(head == null){
            return true;
        }
        ListNode fast = head,slow = head;
        while(fast!=null){
            slow = slow.next;
            fast = fast.next != null ? fast.next.next : fast.next ;
        }
        ListNode subHead = head;
        ListNode subHead2 = reverseList(slow);
        while(subHead2 != null){
            if(subHead.val != subHead2.val ){
                return false;
            }
            subHead = subHead.next;
            subHead2 = subHead2.next;
        }
        return true;
    }

    public ListNode reverseList(ListNode head){
        if(head == null){
            return null;
        }
        ListNode pre = null;
        ListNode next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }


}
